﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smoke
{
    class AutoWindowsize
    {
      private  double formOriginalwidth;//窗口宽度
      private  double formOriginalheight;//窗口高度
      private double scalex;
      private double scaley;
      private  Dictionary<string, string> controlsinfo = new Dictionary<string, string>();
      private Form _form;
      private  Panel win_panel = new Panel();
      public AutoWindowsize(Form form)
        {
            _form = form;
            _form.Controls.Add(win_panel);
            win_panel.BorderStyle = BorderStyle.None;
            win_panel.Dock = DockStyle.Fill;
            while (_form.Controls[0].Name.Trim()!="")
            {
                foreach (Control item in _form.Controls)
                {
                    if (item.Name.Trim()!=""&&item.Name.Trim()!=win_panel.Name.Trim())
                    {
                        win_panel.Controls.Add(item);
                    }

                }
            }
            InitControlsInfo(win_panel);
        }

        private void InitControlsInfo(Control mcontrol)
        {
            if(mcontrol.Parent==_form)
            {
                formOriginalheight = Convert.ToDouble(mcontrol.Height);
                formOriginalwidth = Convert.ToDouble(mcontrol.Width);
            }
            foreach (Control item in mcontrol.Controls)
            {
                if (item.Name.Trim()!="")
                {
                    controlsinfo.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2) + "," + item.Width + "," + item.Height + "," + item.Font.Size);
                    
                }
                if ((item as UserControl)==null&&item.Controls.Count>0)
                {
                    InitControlsInfo(item);
                }
            }
        }
        public void FormSizeChange()
        {
            try
            {
                if (controlsinfo.Count>0)
                {
                    ControlsZoomScale(win_panel);
                    ControlChange(win_panel);

                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void ControlsZoomScale(Control mcontrol)
        {
            scalex = Convert.ToDouble(mcontrol.Width) / formOriginalwidth;
            scaley = Convert.ToDouble(mcontrol.Height) / formOriginalheight;
        }
        private void ControlChange(Control mcontrol)
        {
            double[] pos = new double[5];
            foreach (Control item in mcontrol.Controls)
            {
            if(item.Name.Trim()!="")
                {
                    if ((item as UserControl) == null && item.Controls.Count > 0)
                    {
                        ControlChange(item);
                    }
                    string[] strs = controlsinfo[item.Name].Split(',');
                    for (int i = 0; i < 5; i++)
                    {
                        pos[i] = Convert.ToDouble(strs[i]);
                    }
                    double itemwidth = pos[2] * scalex;
                    double itemheight = pos[3] * scaley;
                    item.Left=Convert.ToInt32(pos[0] * scalex - itemwidth / 2);
                    item.Top = Convert.ToInt32(pos[1] * scaley - itemheight / 2);
                    item.Width = Convert.ToInt32(itemwidth);
                    item.Height = Convert.ToInt32(itemheight);
                    if (float.Parse((pos[4]*Math.Min(scalex,scaley)).ToString())!= 0)
                    {
                        item.Font = new System.Drawing.Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scalex, scaley)).ToString()));
                    }

                }
            }                      
        }
    }
}
